package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityQarTaskPendingMessage;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.dao.entity.QualityQarBasicStandardDao;
import cc.comac.mom.dao.entity.QualityQarCheckInvoiceDao;
import cc.comac.mom.dao.entity.QualityQarTaskPendingMessageDao;
import cc.comac.mom.entity.QualityQarBasicStandardEntity;
import cc.comac.mom.entity.QualityQarBasicStandardRequirementEntity;
import cc.comac.mom.entity.QualityQarCheckInvoiceEntity;
import cc.comac.mom.entity.QualityQarTaskPendingMessageEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class QualityQarTaskPendingMessageDataHandler
		extends ServiceImpl<QualityQarTaskPendingMessageDao, QualityQarTaskPendingMessageEntity> {

	@Autowired
	private QualityQarTaskPendingMessageDao qualityQarTaskPendingMessageDao;

	@Autowired
	private QualityQarBasicStandardDao qualityQarBasicStandardDao;

	@Autowired
	private QualityQarCheckInvoiceDao qualityQarCheckInvoiceDao;

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	// 分页查询当前人员对应的消息提醒
	public ResultBean selectBySomeField(QualityQarTaskPendingMessage qualityQarTaskPendingMessage) {
		ResultBean resultBean = new ResultBean(true);
		if (qualityQarTaskPendingMessage.getPageNum() == 0) {
			qualityQarTaskPendingMessage.setPageNum(1);
		}
		if (qualityQarTaskPendingMessage.getPageSize() == 0) {
			qualityQarTaskPendingMessage.setPageSize(10);
		}

		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("pageNum",
				(qualityQarTaskPendingMessage.getPageNum() - 1) * qualityQarTaskPendingMessage.getPageSize());
		map1.put("pageSize", qualityQarTaskPendingMessage.getPageSize());
		map1.put("user", baseEntityDataUtil.getCurrentUser());
		// 分页查询数据
		List<QualityQarTaskPendingMessageEntity> selectList = qualityQarTaskPendingMessageDao.selectBySomeFields(map1);
		// 查询数据总数量
		int selectCountBySomeFields = qualityQarTaskPendingMessageDao.selectCountBySomeFields(map1);

		Map<String, QualityQarTaskPendingMessage> map = new HashMap<String, QualityQarTaskPendingMessage>();
//		if (selectList.size() > 0) {
		// 通过map接收用来将相同零件号的数据合拼
		List<QualityQarTaskPendingMessage> pendingMessage = new ArrayList<QualityQarTaskPendingMessage>();
		for (QualityQarTaskPendingMessageEntity qualityQarTaskPendingMessageEntity : selectList) {
			QualityQarTaskPendingMessage qualityQarTaskPendingMessage2 = new QualityQarTaskPendingMessage();
			qualityQarTaskPendingMessage2.setInnerData(qualityQarTaskPendingMessageEntity);
			// 判断当前这条数据是否在map中存在相同图号和相同等级的数据
			if (map.containsKey(qualityQarTaskPendingMessageEntity.getPartNumber() + "-"
					+ qualityQarTaskPendingMessageEntity.getLevel())) {
				QualityQarTaskPendingMessage qualityQarTaskPendingMessage3 = map
						.get(qualityQarTaskPendingMessageEntity.getPartNumber() + "-"
								+ qualityQarTaskPendingMessageEntity.getLevel());
				// 依次累加消息内容
				qualityQarTaskPendingMessage3.getInnerData()
						.setContent(qualityQarTaskPendingMessage3.getInnerData().getContent() + ","
								+ qualityQarTaskPendingMessageEntity.getContent());
			} else {
				// 不存在时就新增一个
				QualityQarTaskPendingMessage qualityQarTaskPendingMessage3 = new QualityQarTaskPendingMessage();
				qualityQarTaskPendingMessage3.setInnerData(qualityQarTaskPendingMessageEntity);

				// 查询母本对应数据
				QueryWrapper<QualityQarBasicStandardEntity> queryWrapper2 = new QueryWrapper<QualityQarBasicStandardEntity>();
				queryWrapper2.eq("row_status", 1);
				queryWrapper2.eq("qar_basic_standard_id",
						qualityQarTaskPendingMessageEntity.getQarBasicStandardId());
				// 在对象信息中设置母本状态和当前操作用户
				QualityQarBasicStandardEntity selectOne = qualityQarBasicStandardDao.selectOne(queryWrapper2);
				qualityQarTaskPendingMessageEntity.setStatus(selectOne.getStatus());
				qualityQarTaskPendingMessageEntity.setTaskExecutor(baseEntityDataUtil.getCurrentUser());
				map.put(qualityQarTaskPendingMessageEntity.getPartNumber() + "-"
						+ qualityQarTaskPendingMessageEntity.getLevel(), qualityQarTaskPendingMessage3);
				// 查询当前母本对应的检验单
				QueryWrapper<QualityQarCheckInvoiceEntity> queryWrapper3 = new QueryWrapper<QualityQarCheckInvoiceEntity>();
				queryWrapper3.eq("row_status", 1);
				queryWrapper3.eq("qar_basic_standard_id", selectOne.getId());
				List<QualityQarCheckInvoiceEntity> selectList2 = qualityQarCheckInvoiceDao.selectList(queryWrapper3);
				for (QualityQarCheckInvoiceEntity qualityQarCheckInvoiceEntity : selectList2) {
					if (StringUtils.isEmpty(qualityQarTaskPendingMessageEntity.getEffectQarNumber())) {
						qualityQarTaskPendingMessageEntity.setEffectQarNumber(qualityQarCheckInvoiceEntity.getQarNo());
					} else {
						qualityQarTaskPendingMessageEntity
								.setEffectQarNumber(qualityQarTaskPendingMessageEntity.getEffectQarNumber() + ","
										+ qualityQarCheckInvoiceEntity.getQarNo());
					}
					String user="";
					if (qualityQarCheckInvoiceEntity.getCheckUser() != null) {
						String[] split = qualityQarCheckInvoiceEntity.getCheckUser().split("-");
//						String jobNumber = split[1];
						user = split[0];
					}
					
					String currentUser = baseEntityDataUtil.getCurrentUser();
					if (user.equals(currentUser)) {
						if (StringUtils.isEmpty(qualityQarTaskPendingMessageEntity.getEffectUserQarNumber())) {
							qualityQarTaskPendingMessageEntity
									.setEffectUserQarNumber(qualityQarCheckInvoiceEntity.getQarNo());
						} else {
							qualityQarTaskPendingMessageEntity
									.setEffectUserQarNumber(qualityQarTaskPendingMessageEntity.getEffectUserQarNumber()
											+ "," + qualityQarCheckInvoiceEntity.getQarNo());
						}
					}
				}
			}
		}
		map.forEach((k, v) -> {
			pendingMessage.add(v);
		});

		LimitPageBean<QualityQarTaskPendingMessageEntity> limitPageBean = new LimitPageBean<QualityQarTaskPendingMessageEntity>();
		limitPageBean.setPageCount(
				(int) Math.ceil(selectCountBySomeFields * 1.0d / qualityQarTaskPendingMessage.getPageSize()));
		limitPageBean.setPageNum(qualityQarTaskPendingMessage.getPageNum());
		limitPageBean.setPageSize(qualityQarTaskPendingMessage.getPageSize());
		limitPageBean.setTotalQuantity(selectCountBySomeFields);
		limitPageBean.setEntityList(pendingMessage);
		resultBean.setObject(limitPageBean);
//		}
		return resultBean;
	}

	// 通过试验单数据生成消息提醒数据
	public List<QualityQarTaskPendingMessageEntity> getPendingMessageByTestData(List<QualityQarBasicStandardRequirementEntity> list,String content,QualityQarBasicStandardEntity qarBasicStandard) {
		List<QualityQarTaskPendingMessageEntity> entityList = new ArrayList<QualityQarTaskPendingMessageEntity>();
		if (list.size() > 0) {
			for (QualityQarBasicStandardRequirementEntity qualityQarBasicStandardRequirementEntity : list) {
				QualityQarTaskPendingMessageEntity entity = new QualityQarTaskPendingMessageEntity();
				entity.setContent(content+":"+qualityQarBasicStandardRequirementEntity.getRequirementName());
				entity.setLevel(qarBasicStandard.getVersion());
				entity.setPartName(qarBasicStandard.getPartName());
				entity.setPartNumber(qarBasicStandard.getPartNumber());
				entity.setQarBasicStandardId(qarBasicStandard.getId());
				baseEntityDataUtil.setBaseEntityData(qualityQarBasicStandardRequirementEntity);
				entityList.add(entity);
			}
		}
		return entityList;
	}



}
